package com.ruowei.config;

import com.ruowei.config.enumConfig.StringToEnumConverterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Configure the converters to use the ISO format for dates by default.
 */
@Configuration
public class DateTimeFormatConfiguration implements WebMvcConfigurer {

    private final String uploadDir;

    public DateTimeFormatConfiguration(ApplicationProperties applicationProperties) {
        this.uploadDir = applicationProperties.getFile().getUploadDir();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/app_data/**").addResourceLocations("file:" + "./app_data/");
        registry.addResourceHandler("/photos/**").addResourceLocations("file:" + uploadDir + "/photos/");
        registry.addResourceHandler("/institution/**").addResourceLocations("file:" + uploadDir + "/institution/");
        registry.addResourceHandler("/excel/**").addResourceLocations("classpath:/excel/");
        registry.addResourceHandler("/documents/**").addResourceLocations("file:" + uploadDir + "/documents/");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setUseIsoFormat(true);
        registrar.registerFormatters(registry);
        registry.addConverterFactory(new StringToEnumConverterFactory());
    }
}
